﻿/**************************************************************
单一对象替换:
    配置标示：{%Name}
    actionData：{"Name": "姓名"}
复合对象替换：
    配置标示：{%Select.Name}
    actionData：{ "Select" : { "Name": "姓名"} }
***************************************************************/

(function ($) {

    var actionHelper = {
        getTemplate: function (url) {
            var action = $.ajax({
                url: url,
                async: false
            }).responseText;
            action = $.trim(action.replace(new RegExp("<Root>|</Root>", "ig"), ""));
            return action;
        },
        getAction: function (action, actionData, matchs) {
            var keys = [];
            var newAction = action;
            for (var index = 0; index < matchs.length; index++) {
                var match = matchs[index].replace("{%", "").replace("}", "");
                if ($.inArray(match, keys) > -1) {
                    continue;
                }
                var value = "";
                if (match.indexOf(".") != -1) {
                    var t = match.split('.');
                    value = actionData[t[0]][t[1]];
                } else {
                    value = actionData[match];
                }
                newAction = newAction.replace(new RegExp(matchs[index].replace("%", "\\%"), "ig"), value);
                keys.push(match);
            }
            return newAction;
        },
        getMatchs: function (action) {
            return action.match(/{\%\S+}/ig)
        }
    }

    $.actionHelper = actionHelper;

})(jQuery);